//
//  MyViewController.swift
//  LiuheCommunity
//
//  Created by 罗恒 on 2018/1/17.
//  Copyright © 2018年 luo. All rights reserved.
//

import UIKit

class MyViewController: UIViewController,SelectSegmentDelegate,UITableViewDelegate,UITableViewDataSource,SDWebImageManagerDelegate,UIAlertViewDelegate{
//    var tableData = [["orderNum":"订单编号: A1234567892132313","classImage":"qr.png","name":"阿萨撒奥撒撒撒撒所所撒奥奥奥奥奥奥奥奥奥","condition":"已报名","money":"实付:0.01","QRcode":"qr.png"]]
    var selectindex = 0
    
    var QrVC:QRViewController?
    var model = MyModel()
    
    @IBOutlet weak var exit: UIImageView!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var phone: UILabel!
    @IBOutlet weak var day: UILabel!
    @IBOutlet weak var sex: UILabel!
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var user: UILabel!
    @IBOutlet weak var userImg: UIImageView!
    @IBOutlet var bgview: UIView!
    
    var modelArray:NSMutableArray? = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        

        exit.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(exitActive)))
        exit.isUserInteractionEnabled = true

//分页设置
        let se = Segment.init(frame: CGRect.init(x: 0, y: 290, width: w, height: 30))
        se.delegate = self
        self.bgview.addSubview(se.setSegmentView(arr: ["已报名","进行中","已结束"]))
        
        //创建表视图
        self.tableView!.delegate = self
        self.tableView!.dataSource = self
//        self.tableView!.backgroundColor = UIColor(red: 0xf0/255, green: 0xf0/255,
//                                                  blue: 0xf0/255, alpha: 1)
        
        
        //去除单元格分隔线
        self.tableView!.separatorStyle = .none
        //创建一个重用的单元格
        self.tableView!.register(UINib(nibName:"ActivityTableViewCell", bundle:nil),
                                    forCellReuseIdentifier:"myCell")
        self.bgview.addSubview(tableView!)
        
        
//        jwt
        let isL = UserDefaults.standard.string(forKey: "jwt")
        if isL != nil && isL != ""{

            WebService.shareInstance.jwt = isL
        }

//        WebService.shareInstance.jwt = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwaG9uZU5vIjoiMTU3NTUwMjQwODUiLCJwZXJtaXNzaW9uIjoiMiIsImNyZWF0ZWRBdCI6IjIwMTctMTItMjdUMDI6MjM6MDUuODg0WiIsInVrZXkiOiIxZmQ0OTY2ZS1mMTQwLTQ2OTctYWUxZi1hYjEyNWU4ZTk0OTIiLCJpYXQiOjE1MTYzNTMxMzl9.U2yPztJ46UvM4FnCWTqeqsFgfuYhJRpbD1GX43exQtY"


        
        
        // Do any additional setup after loading the view.
    }
    
  override  func viewWillAppear(_ animated: Bool) {
//    数据请求
        WebService.shareInstance.activityInfo(state: "0") { (isSuccess, par) in

            
            if isSuccess == true{
//                分页判断
                if self.selectindex == 0 {
                    self.modelArray = NSMutableArray.init(array: ShareInfo.shareInstance.array11!)
                }else if self.selectindex == 1 {
                    self.modelArray = NSMutableArray.init(array: ShareInfo.shareInstance.array22!)
                }else if self.selectindex == 2 {
                    self.modelArray = NSMutableArray.init(array: ShareInfo.shareInstance.array33!)
                }

                self.tableView.reloadData()
                
                
                
                
            }else{
                let a = ""
                
                 MBProgressHUD.showToast(to: self.view, withText: a)

            }
        }
    
    WebService.shareInstance.UserInfo{(isSuccess,par)in
        if isSuccess == true{
            let model = ShareInfo.shareInstance.infoAc
            self.user.text = model?.phoneNo
            self.name.text = model?.name
            self.sex.text = model?.sex
            self.day.text = model?.birthDate 
            self.phone.text = model?.phoneNo
            if (self.sex.text == "1"){
                self.sex.text = "男"
            } else if (self.sex.text == "2"){
                self.sex.text = "女"
            }
        }else{
            let a = ""
            
            MBProgressHUD.showToast(to: self.view, withText: a)
            
        }
        
    }
    
    self.tabBarController?.tabBar.isHidden = false
    }
    
    @objc func exitActive(){
 
        let alert = UIAlertView.init(title: "", message: "确定退出登录", delegate:self, cancelButtonTitle: "取消", otherButtonTitles:"确定")
        alert.show()
    }
    
    func alertView(_ alertView:UIAlertView ,clickedButtonAt buttonIndex: Int){
        if buttonIndex == 0 {
            
        }else if buttonIndex == 1{
            ShareInfo.shareInstance.isLogin = false
            UserDefaults.standard.set("", forKey: "jwt")
            UserDefaults.standard.synchronize()
            
            let vc = LoginViewController()
            self.present(vc, animated: false) {
                
            }
        }
    }
    
    
    
//    分页
    func selectSeg(index: Int) {

        selectindex = index
        if self.selectindex == 0 {
            self.modelArray = NSMutableArray.init(array: ShareInfo.shareInstance.array11!)
        }else if self.selectindex == 1 {
            self.modelArray = NSMutableArray.init(array: ShareInfo.shareInstance.array22!)
        }else if self.selectindex == 2 {
            self.modelArray = NSMutableArray.init(array: ShareInfo.shareInstance.array33!)
        }
        self.tableView.reloadData()
        
    }
    
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return modelArray!.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath)
        -> CGFloat {
            return 180
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:ActivityTableViewCell = tableView.dequeueReusableCell(withIdentifier: "myCell")
            as! ActivityTableViewCell
        
//        加载数据
        self.model = (modelArray![indexPath.row] as? MyModel)!
        let picURL = NSURL.init(string: (model.imgMainUrl)!)
        cell.classImage.sd_setImage(with: picURL! as URL)
        cell.orderNum.text = model.out_trade_no
        cell.money.text = model.discountedPrice
        cell.condition.text = model.activityState
        cell.name.text = model.title
        
        cell.QRcode.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(QrActivity)))
        cell.QRcode.isUserInteractionEnabled = true
        
        if(cell.condition.text == "1"){
            cell.condition.text = "已报名"
            cell.condition.textColor = UIColor.orange
        }else if(cell.condition.text == "2"){
            cell.condition.text = "进行中"
            cell.condition.textColor = UIColor.brown
        }else if(cell.condition.text == "3"){
            cell.condition.text = "已结束"
            cell.condition.textColor = UIColor.green
        }
        
        return cell
    }
    
    @objc func QrActivity(){
        
        if (QrVC ==  nil) {
            QrVC = QRViewController()
        }
        let qrVc = QRViewController()
        qrVc.acId = model.activityId
        self.navigationController?.pushViewController(qrVc, animated: true)
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as? ActivityTableViewCell
        cell?.selectionStyle = UITableViewCellSelectionStyle.none
        
        if selectindex == 0 {
            for mod in ShareInfo.shareInstance.array11!{
                let mod1 = mod as? MyModel
                if (mod1?.out_trade_no == cell?.orderNum.text) {
                    let vc = OrderDetailsViewController()
                    vc.model = mod1
                    self.navigationController?.pushViewController(vc, animated: true)
                }
            }
            
        }else if selectindex == 1{
            for mod in ShareInfo.shareInstance.array22!{
                let mod1 = mod as? MyModel
                if (mod1?.out_trade_no == cell?.orderNum.text) {
                    let vc = OrderDetailsViewController()
                    vc.model = mod1
                    self.navigationController?.pushViewController(vc, animated: true)
                    
                }
            }
        }else if selectindex == 2{
            for mod in ShareInfo.shareInstance.array33!{
                let mod1 = mod as? MyModel
                if (mod1?.out_trade_no == cell?.orderNum.text) {
                    let vc = OrderDetailsViewController()
                    vc.model = mod1
                    self.navigationController?.pushViewController(vc, animated: true)
                    
                }
            }
        }
    }
    

}
